#include "HttpServer.hpp"

#define BASE_DIR "./source"

std::string RequestStr(const HttpRequest& req)
{
    std::stringstream str;
    //响应行
    str << req._method << ' ' << req._path << ' ' << req._vision << "\r\n";
    for(auto& e:req._params)
    {
        str << e.first << ": " << e.second << "\r\n";
    }
    for(auto e:req._headers)
    {
        str << e.first << ": " << e.second << "\r\n";
    }
    str << "\r\n";
    //响应内容
    str << req._body;
    return str.str();
}
void Hello(const HttpRequest& req, HttpResponse& rsp)
{
    rsp.SetContent(RequestStr(req), "text/plain");
}
void Login(const HttpRequest& req, HttpResponse& rsp)
{
    rsp.SetContent(RequestStr(req), "text/plain");
}
void PutDir(const HttpRequest& req, HttpResponse& rsp)
{
    std::string path = BASE_DIR + req._path;
    Util::WriteFile(path, req._body);
}
void DelDir(const HttpRequest& req, HttpResponse& rsp)
{
    rsp.SetContent(RequestStr(req), "text/plain");
}

int main()
{
    HttpServer server(8888);
    server.SetThreadCount(3);
    server.SetBaseDir(BASE_DIR);
    server.Get("/hello", Hello);
    server.Post("/login", Login);
    server.Put("/1234.txt", PutDir);
    server.Delete("/1234.txt", DelDir);
    server.Listen();
    return 0;
}